<?php 
include_once 'viper/autoloader.php';

class viper_server_dbOps {
  
  public static function ReadFromUserFeatureScoreDatabase() {
    $sql = 'select * from userFeatureScore';
    return viper_dao::query($sql);
  }
  
  public static function WriteToUserQuicklist($friends) {
    $n = viper_dao::getParameter('user_number');     // number of users
	$m = viper_dao::getParameter('feature_number');      // number of features 
	$k = viper_dao::getParameter('cluster_number');       // # of clusters
	$f = viper_dao::getParameter('friends_number');      // # of friends
    
    for ($i = 0; $i < $n; $i++) {
      $table_name = 'userquicklist';
      $sql = 'UPDATE '.$table_name;
      $sql = $sql.' SET ';
      for ($j = 0; $j < $f; $j++) {
        $sql = $sql."friend".$j.'='.$friends[$i][$j];
        if ($j != $f - 1)
          $sql = $sql.', ';
      }
      $sql = $sql.' WHERE userid='.($i+1);
      // echo $sql;
      viper_dao::query($sql);
    }
  }
  
  public static function ReadFromUserQuicklist() {
    $sql = 'select * from userquicklist';
    return viper_dao::query($sql);
  }
  
  public static function ReadFromGlobalAverage() {
    $sql = 'select * from globalaverage';
    return viper_dao::query($sql);
  }
  
  public static function AddFeatureColumn() {
  	// Get parameters
  	$m = viper_dao::getParameter('feature_number');
  	
  	// Insert column into table
  	$sql = 'ALTER TABLE "userFeatureScore"';
  	$sql = $sql.' ADD "feature'.$m.'" INT NOT NULL';
  	
  	// Update parameters
  	viper_dao::setParameter('feature_number', $m+1);
  }
  
  public static function AddNewFeatureScore($scores) {
  	// Add the scores to the table
  }

  public static function DeleteFeatureColumn($featureIndex) {
  	// Delete column from table
  	$sql = 'ALTER TABLE "userFeatureScore"';
  	$sql = $sql.' ADD "feature'.$featureIndex.'"';
  	 
  	// Update parameters
  	$m = viper_dao::getParameter('feature_number');
  	viper_dao::setParameter('feature_number', $m-1);
  }
  
  public static function AddUserRow($userValue, $userScores, $userFriends) {
  	// Get parameters
  	$n = viper_dao::getParameter('user_number');
  	
  	// Insdert row into table
  	$sql = 'INSERT INTO userFeatureScore';
  	$sql = $sql.' VALUES (user'.$n;
  	for ($i = 1; $i <= $m; $i++) {
  		$sql = $sql.', ';
  		$sql = $sql.$userScores[feature.$i];
  	}
  	$sql = $sql.')';
  	
  	viper_dao::query($sql);
  	
  	// Update parameters
  	viper_dao::setParameter('user_number', $n+1);
  	
  	// Update the friend list
  		
  }
  
  public static function AddNewUserInfo() {
  	// *FIXME* this needs to be improved
  	// Assign him to a cluster at random
  	// Add the scores to the table
  }
  
  
} // end of viper_server_dbOps

?>
  